import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
// import { getRouter } from "@/api/overtrue";
import { useMenusStore } from "@/store/index";
import type { App } from "vue";
import pinia from "@/store/store";
const menusStore = useMenusStore(pinia);
let routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("../view/workers.vue"),
    meta: {
      title: "教代会",
    },
  },
  {
    path: "/mechanism",
    component: () => import("../view/mechanism/index.vue"),
    meta: {
      title: "机构设置",
    },
  },
  {
    path: "/wangEditor",
    component: () => import("../view/common/wangEditor.vue"),
    meta: {
      title: "wangEditor",
    },
  },
  {
    path: "/contingent",
    component: () => import("../view/contingent/index.vue"),
    meta: {
      title: "代表团设置",
    },
  },
  {
    path: "/management",
    component: () => import("../view/management/index.vue"),
    meta: {
      title: "代表管理",
    },
  },
  {
    path: "/overtrueConfig",
    component: () =>
      import("../view/overture/overtureConfig/overtureConfig.vue"),
    meta: {
      title: "提案状态管理",
    },
  },
  {
    path: "/setOvertrueConfig",
    component: () =>
      import("../view/overture/overtureConfig/setOvertrueConfig.vue"),
    meta: {
      title: "提案配置管理",
    },
  },
  {
    path: "/uploadFile",
    component: () => import("../view/democracy/disclosure.vue"),
    meta: {
      title: "公开列表",
    },
  },
  {
    path: "/disclosure/apply",
    component: () => import("../view/democracy/apply.vue"),
    meta: {
      title: "公开申请",
    },
  },
  {
    path: "/disclosure/applyMessage",
    component: () => import("../view/democracy/applyMessage.vue"),
    meta: {
      title: "留言回复",
    },
  },
  {
    path: "/disclosure/MyMessage",
    component: () => import("../view/democracy/MyMessage.vue"),
    meta: {
      title: "我的申请",
    },
  },
  {
    path: "/workHome",
    component: () => import("../view/workHome/workHome.vue"),
    meta: {
      title: "教工之家",
    },
  },
  {
    path: "/cockpit",
    component: () => import("../view/cockpit/cockpit.vue"),
    meta: {
      title: "驾驶舱",
    },
  },
  {
    path: "/SecondLevelCongress/request",
    component: () => import("../view/SecondLevelCongress/request.vue"),
    meta: {
      title: "会议请示",
    },
  },
  {
    path: "/SecondLevelCongress/approval",
    component: () => import("../view/SecondLevelCongress/approval.vue"),
    meta: {
      title: "会议批复",
    },
  },

  {
    path: "/SecondLevelCongress/list",
    component: () => import("../view/SecondLevelCongress/list.vue"),
    meta: {
      title: "会议阅览",
    },
  },
  // 经费管理模块
  {
    path: "/funds/budget/allocate",
    component: () => import("../view/funds/budget/allocate.vue"),
    meta: {
      title: "经费下拨",
    },
  },
  {
    path: "/funds/budget/remainder",
    component: () => import("../view/funds/budget/remainder.vue"),
    meta: {
      title: "额度查询",
    },
  },
  {
    path: "/funds/budget/budgetApply",
    component: () => import("../view/funds/budget/budgetApply.vue"),
    meta: {
      title: "预算申请",
    },
  },
  {
    path: "/funds/budget/budgetAprove",
    component: () => import("../view/funds/budget/budgetAprove.vue"),
    meta: {
      title: "预算审批",
    },
  },
  {
    path: "/funds/expense/expenseApply",
    component: () => import("../view/funds/expense/expenseApply.vue"),
    meta: {
      title: "报销申请",
    },
  },
  {
    path: "/funds/expense/expenseApprove",
    component: () => import("../view/funds/expense/expenseApprove.vue"),
    meta: {
      title: "报销审批",
    },
  },
  {
    path: "/funds/expense/expenseRecord",
    component: () => import("../view/funds/expense/expenseRecord.vue"),
    meta: {
      title: "报销记录",
    },
  },
  {
    path: "/funds/contribution",
    component: () => import("../view/funds/contribution/contribution.vue"),
    meta: {
      title: "会员缴费",
    },
  },

  {
    path: "/funds/charitable",
    component: () => import("../view/funds/charitable/charitable.vue"),
    meta: {
      title: "慈善捐款",
    },
  },
  {
    path: "/demo",
    component: () => import("../view/demo.vue"),
    meta: {
      title: "demo",
    },
  },
  // {
  //   path: "/:pathMatch(.*)*",
  //   redirect: "/",
  //   meta: {
  //     title: "提案状态管理",
  //   },
  // },
];

const views = import.meta.glob("@/view/**/*.vue");

const resolveComponent = (component: string) => {
  const comp = views[`/src/view/${component}.vue`];
  if (!comp) {
    throw new Error("组件未找到");
  }
  return comp;
};
/** 发送请求获取路由 */
await menusStore.getMenusSync();
const routers = menusStore.menus.map((item: any) => {
  const component = resolveComponent(item.component);
  return {
    ...item,
    meta: {
      title: item.name,
      menuId: item._id,
    },
    component,
  };
});

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

routers.forEach((Route: any) => {
  router.addRoute(Route);
});
// 导航守卫
router.beforeEach((to, _, next) => {
  // 路由发生变化修改页面title
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }
  next();
});

// config router
export function setupRouter(app: App<Element>) {
  app.use(router);
}
